package demo02;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class test3 {
	public static void main(String[] args) {
        List<String> students = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader("names.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                students.add(line);
            }
        } catch (IOException e) {
            e.getMessage();
            return;
        }

        File roundFile = new File("round.txt");
        int round = 0;
        try {
            if (!roundFile.exists()) {
                roundFile.createNewFile();
            }

            try (BufferedReader roundReader = new BufferedReader(new FileReader(roundFile))) {
                String roundStr = roundReader.readLine();
                if (roundStr != null) {
                    round = Integer.parseInt(roundStr);
                }
            } catch (IOException e) {
                e.getMessage();
            }

            Random random = new Random();
            String selectedStudent = null;
            int randomIndex;
            randomIndex = random.nextInt(students.size());
            selectedStudent = students.get(randomIndex);
            round++;
            try (FileWriter roundWriter = new FileWriter(roundFile)) {
                roundWriter.write(String.valueOf(round));
            } catch (IOException e) {
                e.getMessage();
            }

            if (round % 3 == 0) {
                System.out.println("随机学生为: 张三-男-20");
            } else {
                System.out.println("随机学生为: " + selectedStudent);
            }
        } catch (IOException e) {
            e.getMessage();
        }
    }
}